<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-select v-model="listQuery.Level" placeholder="预警等级" size="default" @change="searchForm" clearable class="w_150">
					<el-option v-for="item in state.warningLevelList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
				<el-input
					size="default"
					class="w_150 ml10"
					v-model="listQuery.Name"
					clearable
					@clear="searchForm"
					placeholder="预警名称"
					@keydown.enter="searchForm"
				>
				</el-input>
				<el-button size="default" @click="searchForm" type="primary" class="ml10"> 查询 </el-button>
				<el-button size="default" type="success" class="ml10" @click="addRule(1)"> 新增 </el-button>
				<el-button size="default" type="info" class="ml10" @click="resetForm"> 重置 </el-button>
				<el-button size="default" :disabled="!multipleSelection.length" @click="onRowDel(2, '')" type="danger" class="ml10"> 批量删除 </el-button>
			</div>
			<tableTem ref="tableRef" v-bind:="state.tableData" @pageChange="onTablePageChange" @onSelectionChange="onSelectionChange">
				<el-table-column type="selection" />
				<el-table-column align="center" type="index" label="序号" width="60" />
				<el-table-column align="center" min-width="120" prop="warningRuleName" label="预警名称"></el-table-column>
				<el-table-column align="center" min-width="100" prop="userName" label="预警等级">
					<template #default="scope">
						<span v-if="scope.row.warningLevel == 3" class="g">高</span>
						<span v-else-if="scope.row.warningLevel == 2" class="z">中</span>
						<span v-else-if="scope.row.warningLevel == 1" class="d">低</span>
						<span v-else>无</span>
					</template>
				</el-table-column>
				<el-table-column align="center" prop="warningDescrption" label="预警说明"></el-table-column>
				<!-- <el-table-column align="center" min-width="120" prop="userName" label="子系统数量"></el-table-column> -->

				<el-table-column align="center" min-width="150" prop="createTime" label="创建时间"></el-table-column>
				<el-table-column label="操作" align="center" width="150" fixed="right">
					<template #default="scope">
						<el-button size="small" type="primary" text @click="editRule(2, scope.row)">编辑</el-button>
						<el-button size="small" type="primary" text @click="detailRule(3, scope.row)">详情</el-button>
						<el-button size="small" type="primary" text @click="onRowDel(1, scope.row.id)">删除</el-button>
					</template>
				</el-table-column>
			</tableTem>
		</div>
		<!-- 弹窗 -->
		<dialogTem @refresh="resetForm()" ref="dialogTemFef" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { WarningRuleFactGetPagedList, WarningRuleFactDelete } from '/@/api/earlyWarning/index';

// 引入组件
const dialogTem = defineAsyncComponent(() => import('./component/dialog.vue'));
const tableTem = defineAsyncComponent(() => import('/@/components/tables/index.vue'));

// 定义变量内容
const tableRef = ref();
const dialogTemFef = ref();
const listQuery = reactive({
	Level: '',
	Name: '',
}) as any;
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: true,
		param: {
			PageIndex: 1,
			PageSize: 10,
		},
	},
	warningLevelList: [
		{
			name: '高',
			id: 3,
		},
		{
			name: '中',
			id: 2,
		},
		{
			name: '低',
			id: 1,
		},
		{
			name: '无',
			id: 0,
		},
	],
});
// 页面加载时
onMounted(() => {
	getTableData();
});
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const params = {
			...state.tableData.param,
			...listQuery,
		};
		if (!listQuery.Level) delete params.Level;
		if (!listQuery.Name) delete params.Name;
		let data = await WarningRuleFactGetPagedList(params);
		state.tableData.data = data.data.items;
		state.tableData.total = data.data.totalCount;
	} finally {
		state.tableData.loading = false;
	}
};

// 新增
const addRule = (type: any) => {
	dialogTemFef.value.openDialog(type);
};
// 编辑
const editRule = (type: any, row: any) => {
	dialogTemFef.value.openDialog(type, row);
};
// 编辑
const detailRule = (type: any, row: any) => {
	dialogTemFef.value.openDialog(type, row);
};

// 分页改变
const onTablePageChange = (val: any) => {
	state.tableData.param.PageIndex = val.pageNum;
	state.tableData.param.PageSize = val.pageSize;
	getTableData();
};
// 多选数据
const multipleSelection = ref([]);
// 多选
const onSelectionChange = (val: any) => {
	multipleSelection.value = val;
};

// 删除预警
const onRowDel = (type: any, id: any) => {
	ElMessageBox.confirm(`此操作将永久删除预警配置，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		const ids = [] as any;

		if (type == 1) {
			ids.push(id);
		} else {
			if (multipleSelection.value.length === 0) {
				ElMessage({
					type: 'warning',
					message: '请选择要删除的数据',
				});
				return;
			}
			multipleSelection.value &&
				multipleSelection.value.map((item: any) => {
					ids.push(item.id);
				});
		}

		await WarningRuleFactDelete(ids);
		ElMessage.success('删除成功');
		getTableData();
	});
};
// 搜索点击时表单回调
const searchForm = () => {
	state.tableData.param.PageIndex = 1;
	tableRef.value.pageReset();
};
// 重置
const resetForm = () => {
	listQuery.Level = '';
	listQuery.Name = '';
	searchForm();
};
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
.g {
	color: red;
}
.z {
	color: orange;
}
.d {
	color: blue;
}
</style>


